package me.ao3dolydemo3;

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.eat();
//      if(a instanceof Dog){
//          Dog d=(Dog)a;
//          d.sleep();
//      }
//      else if(a instanceof Cat){
//          Cat c=(Cat)a;
//          c.sleep();
//      }

        if (a instanceof Dog d) {
            d.sleep();
        } else if (a instanceof Cat c) {

            c.sleep();
        }
    }
}

class Animal {
    public void eat() {
        System.out.println("动物吃东西");
    }
}

class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("狗吃骨头");
    }

    public void sleep() {
        System.out.println("狗睡觉");
    }
}

class Cat extends Animal {
    @Override
    public void eat() {
        System.out.println("猫吃老鼠");
    }

    public void sleep() {
        System.out.println("猫睡觉");
    }
}
